package com.example.auth.utils;
import com.example.auth.utils.ResultEnum;
import com.fasterxml.jackson.annotation.JsonIgnore;
import java.io.Serializable;



public class Result<T> implements Serializable {

    private static final long serialVersionUID = 4580737268023862568L;

    private Integer code;

    private String message;

    private T data;

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }


    public Result() {
    }
    public Result(Integer code){
        this.code = code;
    }

    //是否成功（自定义结果码为1000为成功）

    public boolean isSuccess(){
        return this.code == 20000;
    }


    //成功时引用

    public static <T> Result<T> success(){
        return success(ResultEnum.SUCCESS);
    }
    public static <T> Result<T> success(T data){
        return success(ResultEnum.SUCCESS,data);
    }
    public static <T> Result<T> success(ResultEnum re){
        return success(re,null);
    }
    public static <T> Result<T> success(ResultEnum re,T data){
        Integer code = re.getCode();
        String message = re.getMessage();
        return success(code,message,data);
    }
    public static <T> Result<T> success(Integer code, String message, T data){
        Result<T> result = new Result<>(1000);
        result.setCode(code);
        result.setMessage(message);
        result.setData(data);
        return result;
    }



    //失败时引用

    public static <T> Result<T> fail(){
        return fail(ResultEnum.UNKNOWN_ERROR);
    }
    public static <T> Result<T> fail(ResultEnum re){
        return fail(re,null);
    }
    public static <T> Result<T> fail(String msg){
        return fail(ResultEnum.UNKNOWN_ERROR.getCode(),msg,null);
    }
    public static <T> Result<T> fail(T data){
        return fail(ResultEnum.UNKNOWN_ERROR,data);
    }
    public static <T> Result<T> fail(ResultEnum re, T data){
        Integer code = re.getCode();
        String message = re.getMessage();
        return fail(code,message,data);
    }
    public static <T> Result<T> fail(Integer code , String message){
        Result<T> result = new Result<>();
        result.setCode(code);
        result.setMessage(message);
        return result;
    }
    public static <T> Result<T> fail(Integer code , String message, T data){
        Result<T> result = new Result<>();
        result.setCode(code);
        result.setMessage(message);
        result.setData(data);
        return result;
    }


}


